home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Piskvorky / source / piskvork.h < prev    next >
C/C++ Source or Header  |  2006-05-25  |  8KB  |  278 lines

  1. #include <stdio.h>
  2.  
  3. #define myAI "pbrain-pela"
  4. #define fnHtmlTable "_table.html"
  5. #define fnResult "_result.txt"
  6. #define fnState "state.tur"
  7. #define fnStateTmp "turstate.tmp"
  8. #define fnMsg "messages.txt"
  9. #define fnOpenings "openings.txt"
  10.  
  11. #define Mplayer 127
  12. #define NET_PLAYER 0
  13.  
  14. typedef int Tsymbol;
  15. struct Tsquare;
  16. typedef Tsquare *Psquare;
  17. typedef char TfileName[256];
  18. typedef char TdirName[224];
  19. enum{ INFO_TIMEMOVE=1,INFO_TIMEGAME=2,INFO_MEMORY=4,INFO_RULE=8,
  20.   INFO_GAMETYPE=16,INFO_TIMELEFT=32,INFO_DIR=64, INFO_ALL=0xffff };
  21.  
  22.  
  23. struct Tsquare                   
  24. {
  25.   Tsymbol z;      //0=nothing, 1=X, 2=O, 3=outside
  26.   Psquare nxt,pre;//linked list for undo, redo
  27.   int x,y;        //coordinates <1..width, 1..height>
  28.   int time;       //total thinking time
  29.   Psquare winLineStart;//0 or beginning of a winning line
  30.   int winLineDir; //direction offset of a winning line
  31. };
  32.  
  33. struct Txyp {
  34.   int x,y;
  35.   Psquare p;
  36. };
  37.  
  38. struct ThreadItem
  39. {
  40.   ThreadItem *nxt,*prv;
  41.   HANDLE h;
  42.   DWORD id;
  43.   ThreadItem(){ nxt=prv=this; }
  44.   void init(){ nxt=prv=this; }
  45.   ~ThreadItem(){ prv->nxt= nxt; nxt->prv= prv; }
  46.   void append(ThreadItem *item);     
  47.   int isEmpty(){ return nxt==this; }
  48. };
  49.  
  50. struct Tplayer
  51. {
  52.  int isComp;   //0=human, 1=computer
  53.  int timeMove; //time for a move (ms)
  54.  int timeGame; //time for a game (s)
  55.  int score;    //number of wins
  56.  int time;     //total time (ms)
  57.  int timeInit; //time of initialization, -1 during initialization, 0 after the first move
  58.  int memory;   //used memory (bytes)
  59.  int turPlayerId;  //number of player in tournament
  60.  int Nmoves;       //moves counter
  61.  HANDLE process;   //process handle
  62.  DWORD processId;  //process identifier
  63.  HANDLE pipeToAI;  //pipe handle
  64.  HANDLE pipeFromAI;//pipe handle
  65.  HANDLE dbgThread;   //debugger thread (only pipe protocol)
  66.  ThreadItem threads; //list of threads (only pipe protocol)
  67.  Txyp *lastBoard;
  68.  int DlastBoard;
  69.  bool mustSendBoard; //whole board must be send to AI
  70.  bool isTimeOut;     //time out
  71.  bool mustSendInfo;  //INFO timeout_turn must be send to AI
  72.  TfileName brain;    //name of the EXE or ZIP file
  73.  TfileName brain2;   //name of the EXE file
  74.  TdirName temp;      //temporary folder 
  75.  
  76.  int level();
  77.  void getName(char *buf, int n);
  78.  char *getFilePart(); 
  79.  void rdBrain(FILE *f);
  80.  int timeLeft();
  81.  void checkTimeOut(int currentTime);
  82.  int move();
  83.  void getMem();
  84.  int createProcess(bool pipe, STARTUPINFO &si);
  85.  void stopAI();
  86.  void startPipeAI();
  87.  bool notRunning();
  88.  void sendCommand(char *text,...);
  89.  int sendInfo(int mask=INFO_ALL);
  90.  void sendInfoCmd(char *key, int value);
  91.  void sendInfoCmd(char *key, char *value);
  92.  int storeBoard();
  93.  int readCommand(char *buf, int n);
  94.  int brainChanged();
  95.  void wrMsg(char *s);
  96.  void wrDebugLog(char *s);
  97.  void openThreads();
  98.  
  99.  FILE *infoDatFile;
  100. };
  101.  
  102. struct TturPlayer
  103. {
  104.   int wins,losses,timeouts,errors,wins1,winsE,losses1;
  105.   int time,memory,maxTurnTime;
  106.   int Nmoves,Ngames;
  107.   DWORD crc;
  108.   int points;
  109.   float ratio;
  110. };
  111.  
  112. struct TturCell
  113. {
  114.   short start,notStart,error;
  115.   int sum(){ return start+notStart+error; }
  116. };
  117.  
  118. struct Tclient {
  119.   SOCKET socket;
  120.   HANDLE thread;
  121.   int player[2];
  122.   int repeatCount;
  123.   int gameCount;
  124.   int opening;
  125.   u_long IP;
  126.   void gameFinished();
  127. };
  128.  
  129. struct PROCESS_MEMORY_COUNTERS {
  130.   DWORD cb,PageFaultCount,PeakWorkingSetSize,WorkingSetSize,
  131.     QuotaPeakPagedPoolUsage,QuotaPagedPoolUsage,
  132.     QuotaPeakNonPagedPoolUsage,QuotaNonPagedPoolUsage,
  133.     PagefileUsage,PeakPagefileUsage;
  134. };
  135. typedef BOOL (__stdcall *TmemInfo)(HANDLE,PROCESS_MEMORY_COUNTERS*,DWORD);
  136.  
  137. //-----------------------------------------------------------------
  138.  
  139. extern int player,moves,width,height,tolerance,maxMemory,turNplayers,logDebug,logMessage,suspendAI,debugAI,port,turCurRepeat,autoBegin,turRepeat,turRecord,turDelay,priority,terminate,turGamesCounter,turTieRepeat,turTieCounter,startMoves,errDelay,turOnlyLosses,turFormat,turGamesTotal,turOpening,invert,turNet,turRule,mx,my,height2,lastTurnTime[2],hardTimeOut,humanTimeOut,opening,logMoves,moveStart,coordStart,sameTime,turMatchRepeat,turLogMsg,infoEval,saveLock,debugPipe,exactFive,continuous,undoRequest,netGameVersion,ignoreErrors;
  140. extern bool paused,finished,disableScore,isWin9X,isClient,isServer,isNetGame,isListening,turTimerAvail,levelChanged,cmdLineGame;
  141. extern Psquare lastMove,board,boardb,boardk,hilited;
  142. extern DWORD lastTick;
  143. extern TturPlayer *turTable;
  144. extern TturCell *turCells;
  145. extern Tplayer players[2];
  146. extern HANDLE thread,pipeThread,endGameEvent;
  147. extern char *title;
  148. extern TfileName fnstate,TahDat,PlochaDat,TimeOutsDat,MsgDat,InfoDat;
  149. extern TdirName fntur,tempDir,AIfolder,dataDir;
  150. extern char servername[128],turPlayerStr[50000],cmdGameEnd[512],cmdTurEnd[512];
  151. extern CRITICAL_SECTION threadsLock,netLock,timerLock,infoLock;
  152. extern SYSTEMTIME turDateTime; 
  153. extern HWND hWin,msgWnd,logDlg,resultDlg;
  154. extern SOCKET sock,sock_l;
  155. extern HMODULE psapi;
  156. extern TmemInfo getMemInfo;
  157. extern Tclient clients[Mplayer];
  158.  
  159. //-----------------------------------------------------------------
  160.  
  161. int vmsg(char *caption, char *text, int btn, va_list v);
  162. int msg1(int btn, char *text, ...);
  163. void wrLog(char *text, ...);
  164. void vwrLog(char *text, va_list ap);
  165. void writeini();
  166. int close(FILE *f,char *fn);
  167. void show(HWND wnd);
  168. void paintSquare(Psquare p);
  169. void printLevel();
  170. void printLevel1();
  171. void printScore();
  172. void printMoves();
  173. void printTime(int pl);
  174. bool getWinLine(Psquare p, Psquare &p2);
  175. void printWinLine(Psquare p);
  176. void invalidate();
  177. void hiliteLast();
  178. void cancelHilite();
  179. Psquare Square(int x,int y);
  180. void browseFolder(char *buf,HWND wnd,int item,char *txt);
  181. void drawTitle();
  182. void setPriority(HANDLE process);
  183. unsigned rnd(unsigned n);
  184. TturCell *getCell(int loser,int winner);
  185. bool cmpExt(char *s,char *e);
  186. void delDir(char *path,bool d);
  187. int checkDir(HWND hDlg, int item);
  188. DWORD WINAPI threadLoop(LPVOID);
  189. DWORD WINAPI pipeThreadLoop(LPVOID);
  190.  
  191. void turGetBrain(int i, char *out, int n, bool noPath);
  192. char *getTurDir(char *buf);
  193. void getMsgFn(char *fn);
  194. DWORD searchAI(const char *src, int n, char *dest, char **filePart);
  195. void wrMsgNewGame(int p1,int p2);
  196. void wrMessage(char *fmt,...);
  197. void executeCmd(char *cmd);
  198. int getNplayers();
  199. void initOpeningTab();
  200. void shutOpeningTab();
  201. signed char* getOpening(int i);
  202. void turStart();
  203. void turRestart();
  204. void turNext();
  205. bool turNext(Tclient *client);
  206. void turLocalNext();
  207. void turResult();
  208. void turGameFinished();
  209. bool doMove(Psquare p);
  210. bool doMove1(Psquare p, int action=0);
  211. void init();
  212. void resetScore();
  213. void newGame(int pl, bool openingEnabled);
  214. void restartGame();
  215. void resume();
  216. void softPause();
  217. void hardPause();
  218. void killBrains();
  219. int stopThinking();
  220. void finishGame();
  221. void turAddTime();
  222. void saveRec();
  223. void wrGameResult();
  224. void sendInfoEval();
  225. void boardChanged();
  226. bool notSuspended();
  227. bool redo();
  228. bool undo();
  229. void setLevel(int l,int h);
  230. void switchPlayer(int h1, int h2);
  231. void savePsq(char *fn, int format);
  232. void openPsq(char *fn);
  233. DWORD getTickCount();
  234.  
  235. int initWSA();
  236. int startListen();
  237. int startConnection();
  238. int clientStart();
  239. void clientEnd();
  240. void killClient(int i);
  241. void turAbort();
  242. void stopListen();
  243. void serverEnd();
  244. int serverStart();
  245. void clientFinished();
  246. int calcCRC(const char *fn, DWORD &result);
  247. void wrState();
  248. int rdState();
  249. int rd(char *buf, int len);
  250. int rd1();
  251. int rd2();
  252. int rd4();
  253. int wr(char *buf, int len);
  254. int wr1(SOCKET s, int i);
  255. int wr1(int i);
  256.  
  257. int netGameStart();
  258. void netGameEnd();
  259. void newNetGame(LPARAM lP);
  260. void netGameMove(Psquare p);
  261. void netGameMsg();
  262. void netGameUndo();
  263. void netGameNew();
  264.  
  265. //-----------------------------------------------------------------
  266.  
  267. #define nxtP(p,i) (p=(Psquare)(((char*)p)+(i*s)))
  268. #define prvP(p,i) (p=(Psquare)(((char*)p)-(i*s)))
  269. #define nxtS(p,s) ((Psquare)(((char*)p)+diroff[s]))
  270.  
  271. template <class T> inline void amin(T &x,int m){ if(x<m) x=m; }
  272. template <class T> inline void amax(T &x,int m){ if(x>m) x=m; }
  273. template <class T> inline void aminmax(T &x,int l,int h){
  274.  if(x<l) x=l;
  275.  if(x>h) x=h;
  276. }
  277.  
  278.